package com.example.myapplication.pdf;

import android.graphics.Bitmap;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;

import com.example.myapplication.utils.LogUtils;

public class WebviewUtils {
    public static WebviewUtils webviewUtils;

    public static WebviewUtils init() {
        if(webviewUtils==null){
            webviewUtils = new WebviewUtils();
        }
        return webviewUtils;
    }

    /**
     * 初始化WebView
     */
    public void initWebView(WebView webView) {
        setWebView(webView);
        setWebViewMonitor(webView);
    }

    /**
     * 添加webview监听
     *
     * @param mWebView
     */
    private void setWebViewMonitor(final WebView mWebView) {
        mWebView.setWebViewClient(new WebViewClient() {
            @Override
            public void onPageStarted(WebView view, String url, Bitmap favicon) {
                super.onPageStarted(view, url, favicon);
                LogUtils.i("dove", "开始请求" + url);
            }

            @Override
            public void onPageFinished(WebView view, String url) {
                super.onPageFinished(view, url);
                LogUtils.i("dove", "当前请求" + url);
//                if (!isError) {
//                    isSuccess = true;
//                    //回调成功后的相关操作
//                } else {
//                    setShowFailed(true);
//                }
//                isError = false;
            }

//            @Override
//            public void onReceivedError(WebView view, WebResourceRequest request, WebResourceError error) {
//                super.onReceivedError(view, request, error);
//                isError = true;
//                isSuccess = false;
//                //回调失败的相关操作
//            }

            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                LogUtils.i("dove", "url-" + url);
                view.loadUrl(url);
//                if (url.startsWith("http://") || url.startsWith("https://")) {
//                    view.loadUrl(url);
//                } else {
//                    Intent intent = new Intent();
//                    intent.setAction(Intent.ACTION_VIEW);
//                    intent.setData(Uri.parse(url));
//                    try {
//                        startActivity(intent);
//                    } catch (ActivityNotFoundException e) {
//                        e.printStackTrace();
//                        return false;
//                    }
//                }
                return true;
            }
        });
        mWebView.setWebChromeClient(new WebChromeClient() {
            @Override
            public void onProgressChanged(WebView view, int newProgress) {
                super.onProgressChanged(view, newProgress);
                LogUtils.i("dove", "当前加载进度" + newProgress);
            }
        });
    }

    /**
     * webview常规设置
     */
    private void setWebView(WebView mWebView) {
        WebSettings webSettings = mWebView.getSettings();
        webSettings.setJavaScriptEnabled(true);//设置能够解析Javascript
        webSettings.setDomStorageEnabled(true);//设置适应Html5 //重点是这个设置
        //设置字符编码
        webSettings.setDefaultTextEncodingName("utf-8");
        // 支持缩放
        webSettings.setSupportZoom(false);
        // //启用内置缩放装置
        webSettings.setBuiltInZoomControls(false);
        // 支持自动加载图片
        webSettings.setLoadsImagesAutomatically(true);
        // 支持内容重新布局
        webSettings.setLayoutAlgorithm(WebSettings.LayoutAlgorithm.SINGLE_COLUMN);

        webSettings.setAllowFileAccess(true);// 设置允许访问文件数据
        webSettings.setJavaScriptCanOpenWindowsAutomatically(true);
        webSettings.setCacheMode(WebSettings.LOAD_CACHE_ELSE_NETWORK); //设置缓存
        webSettings.setDomStorageEnabled(true);
        webSettings.setDatabaseEnabled(true);
    }

}
